<template>
  <CompOperate
    :field="field"
    :designer="designer"
    :design-state="designState"
    :parent-list="parentList"
    :parent-widget="parentWidget"
    :sub-form-row-id="subFormRowId"
    :sub-form-row-index="subFormRowIndex"
    :sub-form-col-index="subFormColIndex"
    :index-of-parent-list="indexOfParentList"
    :display-style="field.options.displayStyle"
  >
    <div class="scene-public-doctor-scheduling-widget" :style="getStyle('head')">
      <!--医生信息-->
      <template v-if="true">
        <template v-for="(item,index) in getRoomDoctorInfo">
          <div class="doctor-info" :key="index">
            <div class="picture">
              <img alt="医生" @error='handleError' :src="getDoctorPicture(item)" >
            </div>
            <div class="doc-info-warp">
              <!--医生姓名职称-->
              <div class="info">
                <div class="name">{{ item?.workerName || '\u00a0' }}</div>
                <div v-if="item?.workerTitle" class="level" :style="getStyle('doctorWorkerTitleSize')">
                  {{ item?.workerTitle || "\u00a0" }}
                </div>
              </div>
              <!--医生简介-->
              <div class="doctor-describe">
                <ScrollY :content="(item?.workerIntro) || '\u00a0'" :pageDuration="10"/>
              </div>
            </div>
          </div>

        </template>
      </template>
    </div>
  </CompOperate>
</template>

<script>
import {pxToVw} from '@/utils/common';
import emitter from "@/utils/designer/emitter";
import {buildParentApi} from "@/utils/designer/util";
import ScrollY from '@c/public/seamlessScroll/ScrollY.vue'
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import ScrollX from "@c/public/seamlessScroll/ScrollX.vue";
import {v4 as uuid} from "uuid";

let buildParent = buildParentApi();
export default {
  name: "scene-public-doctor-scheduling-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    ScrollX,
    CompOperate,
    ScrollY
  },

  props: {
    widget: Object,
    devType: {
      type: Boolean,
      default: false,
    },
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },
    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi()
    },
  },

  data() {
    return {
      temp: null,
      states: true,
      optionsInfo:Array(),
      widgetKey: uuid(),
      header2: require("@a/images/design/header.png"),
      header: require("@a/images/design/header.jpeg"),
      pause: require("@a/images/design/pause.png"),
    }
  },

  computed: {
    // 设置的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Array();
        let Obj = Object();
        switch (val) {
          case 'head':
            Reflect.set(Obj, 'height', pxToVw(info?.headHeight));
            Reflect.set(Obj, 'backgroundColor', `${(info?.backgroundColor)}`);
            return Obj;
          case 'doctorInfo':
            Reflect.set(Obj, 'fontWeight', (info?.doctorInfoFontWeight));
            Reflect.set(Obj, 'fontSize', pxToVw(info?.workerNameFontSize));
            return Obj;
          case 'doctorWorkerTitleSize':
            Reflect.set(Obj, 'fontSize', pxToVw(info?.workerTitleFontSize));
            Reflect.set(Obj, 'fontWeight', (info?.doctorInfoFontWeight));
            return Obj;
          case 'doctorDescribe':
            Reflect.set(Obj, 'fontSize', pxToVw(info?.doctorIntroduceContentFontSize));
            return Obj;
        }
      }
    },

    // 获取全量医患信息
    getRoomDoctorInfo() {
      let devType = (this.devType);
      let {patientListTotal= 4} = this.optionsInfo;
      let roomDoctorInfo = this.$store.getters['getPublishGeneralListInfo'];
      let queueList = devType? roomDoctorInfo.apiData :roomDoctorInfo.mockData;
      if (!!(queueList?.length)) {
        if ((queueList.length) >= patientListTotal) {
          return queueList.slice(0, patientListTotal);
        } else {
          let arr = (new Array(patientListTotal - queueList.length).fill({}));
          return [...queueList, ...arr];
        }
      } else {
        return new Array(patientListTotal).fill({})
      }
    },

    // 获取医生头像
    getDoctorPicture() {
      return function (row) {
        let header = "/tpleditor/resource/assets/images/header.png";
        return row?.workerPicture || header;
      }
    },
  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.handleOnMounted();
    // this.executeMethod('publishListInfo',resp)
  },

  methods: {
    // 接收父组件传递过来的数据
    async executeMethod(key, data) {
      switch (key) {
        case "publishListInfo":
          this.widgetKey = uuid();
          let publicListInfo = this.$lodash.cloneDeep(data);

          await this.$store.dispatch('setPublicGeneralListInfo', publicListInfo);
          break
      }
    },

    handleError(event) {
      event.target.src = this.header2;
      this.states = false;
    }
  },

  watch: {
    field: {
      handler: function (val) {
        let key = val.options.config;
        this.widgetKey = uuid();
        this.optionsInfo = val.options[key];
      },
      deep: true,
      immediate: true
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },

};
</script>

<style lang="scss" scoped>
.scene-public-doctor-scheduling-widget {
  box-sizing: border-box;
  flex: inherit;
  flex-direction: column;
  justify-content: space-between;
  color: #333;
  height: 150px;
  font-size: 40px;
  background: transparent;
  overflow: hidden;

  //医生信息
  .doctor-info {
    display: flex;
    margin: 48px 0 48px 0;
    min-width: auto;
    padding: 0 30px 0 30px;

    .picture {
      flex: inherit;
      border-radius: 20px;
      overflow: hidden;
      width: 264px;
      min-height: 364px;
      max-height: 486px;
      background: transparent;
      flex-shrink: 0;
      border: 1px solid #CCCCCC;
      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    }

    .doc-info-warp {
      padding: 0 0 0 30px;
      width: calc(100% - 264px);
    }
    .info {
      flex: inherit;
      display: flex;
      align-items: center;
      width: calc(100% - 264px);
      border: none;
      background: transparent;

      .name {
        max-width: 268px;
        min-width: 218px;
        font-size: 50px;
        line-height: 70px;
        font-weight: 500;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        box-sizing: border-box;
      }

      .level {
        margin-left: 20px;
        padding: 4px 12px 4px 12px;
        box-sizing: border-box;
        font-size: 30px;
        line-height: 50px;
        background-color: #0059B3;
        color: white;
        border-radius: 10px;
        //max-width: calc(100% - 268px);
        max-width: 400px;
        text-align: center;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
    // 医生介绍
    .doctor-describe {
      font-size: 30px;
      line-height: 1.58;
      padding: 28px 0 0 0;
      height: 260px;
      overflow: hidden;
      color: #666666;
    }
  }

}
</style>

